import datetime


# 传入字符串：09:00-17:30，返回的小时差：8.5
def getWorkingHours(timeStr):
    times = timeStr.split("-")
    startHour = int(times[0][:2])
    startMinute = int(times[0][3:])
    endHour = int(times[1][:2])
    endMinute = int(times[1][3:])

    startTime = startHour * 60 + startMinute
    endTime = endHour * 60 + endMinute
    hourDiff = (endTime - startTime) / 60
    minuteDiff = (endTime - startTime) % 60
    if hourDiff + minuteDiff / 60 < 0:
        raise Exception("上班时间不能大于下班时间")

    return hourDiff + minuteDiff / 60


# 完成的秒
def getPastSeconds(timeStr):
    times = timeStr.split("-")
    start_time = datetime.datetime.strptime(times[0], '%H:%M').time()
    end_time = datetime.datetime.strptime(times[1], '%H:%M').time()
    now_time = datetime.datetime.now().time()

    if now_time < start_time:
        # 如果当前时间早于[上班时间]，则返回0
        return 0
    elif now_time >= end_time:
        # 如果当前时间晚于或等于[下班时间]，则返回[上班时间]到[下班时间]的秒数
        return (datetime.datetime.combine(datetime.date.today(), end_time)
                - datetime.datetime.combine(datetime.date.today(), start_time)).total_seconds()
    else:
        # 否则返回当前时间和[上班时间]之间的秒数
        return (datetime.datetime.combine(datetime.date.today(), now_time)
                - datetime.datetime.combine(datetime.date.today(), start_time)).total_seconds()


def getRunTimeText(timeStr):
    times = timeStr.split("-")
    start_time = datetime.datetime.strptime(times[0], '%H:%M')
    start_time = datetime.datetime.combine(datetime.date.today(), start_time.time())
    end_time = datetime.datetime.strptime(times[1], '%H:%M')
    end_time = datetime.datetime.combine(datetime.date.today(), end_time.time())
    now_time = datetime.datetime.now()
    if now_time < start_time:
        # 如果当前时间早于[上班时间]
        return "没到上班时间"
    elif now_time >= end_time:
        # 如果当前时间晚于[下班时间]
        return "下班啦~"
    else:
        tdelta = end_time - now_time
        return "余：" + ("%02d" % int(tdelta.total_seconds() / 60 / 60)) + ":" + (
                "%02d" % int(tdelta.total_seconds() / 60 % 60))

import sys
import os
def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

